﻿''' <summary>
''' This class implements a TCP port collector.
''' </summary>
''' <remarks>
''' The purpose of the TCP port collector is to attempt to establish a TCP connection
''' to a series of server ports and include the results in the system info report.
''' </remarks>
Public Class TCPPortCollector
    Implements ICollector

    ''' <summary>
    ''' List of writers passed when Startup() is called.
    ''' </summary>
    ''' <remarks></remarks>
    Protected m_writers As List(Of Writer.IWriter)

    ''' <summary>
    ''' List of TCP port entries.
    ''' </summary>
    ''' <remarks></remarks>
    Protected m_entries As List(Of TCPPortEntry)

    ''' <summary>
    ''' This holds an error description.
    ''' </summary>
    ''' <remarks></remarks>
    Protected m_error As String

    Public Sub Cleanup() Implements ICollector.Cleanup

        'Nothing to do

    End Sub

    Public Sub Run() Implements ICollector.Run

        'Write our category name.
        Writer.WritersHelper.WriteCategory("TCP Port Information", m_writers)

        'If an error occurred while reading the configuration file, write
        'it out and stop here.
        If m_error <> "" Then
            Writer.WritersHelper.WriteCategoryError(m_error, m_writers)
        Else
            'Scan all tcp port entries.
            For Each tcp As TCPPortEntry In m_entries
                Dim sock As System.Net.Sockets.TcpClient = Nothing
                Try
                    'Write the server name along with address and port we'll be connecting to.
                    Writer.WritersHelper.WriteSection(tcp.Name + " (" + tcp.Address + ":" + tcp.Port.ToString() + ")", m_writers)
                    'Process this item.
                    sock = New System.Net.Sockets.TcpClient()
                    sock.Connect(tcp.Address, tcp.Port)
                    Writer.WritersHelper.WriteEntry("Connection established", m_writers)
                Catch ex As Exception
                    'Error gathering info.
                    Writer.WritersHelper.WriteSectionError("Error connecting to port (" + ex.Message + ")", m_writers)
                Finally
                    If sock.Connected Then
                        sock.Close()
                    End If
                    sock = Nothing
                End Try
                'Write out a blank.
                Writer.WritersHelper.WriteBlank(m_writers)
            Next
        End If

    End Sub

    Public Sub Startup(ByVal cfgFile As String, ByVal writers As System.Collections.Generic.List(Of Writer.IWriter)) Implements ICollector.Startup

        'Get all writers.
        m_writers = writers
        'Read configuration file.
        ReadConfiguration(cfgFile)

    End Sub

    ''' <summary>
    ''' Read the configuration file.
    ''' </summary>
    ''' <param name="fName">File name with TCP port collector configuration.</param>
    ''' <remarks></remarks>
    Protected Sub ReadConfiguration(ByVal fName As String)

        Const TCPPORT_TABLE As String = "ServerPort"

        Try
            'Read in a dataset.
            Dim ds As New DataSet
            ds.ReadXml(fName)
            m_entries = New List(Of TCPPortEntry)

            'Parse all entries.
            For i As Integer = 0 To ds.Tables(TCPPORT_TABLE).Rows.Count - 1
                With ds.Tables(TCPPORT_TABLE).Rows(i)
                    m_entries.Add(New TCPPortEntry(Convert.ToString(.Item("Name")), _
                                                   Convert.ToString(.Item("Address")), _
                                                   Convert.ToInt32(.Item("Port"))))
                End With
            Next

            ds.Dispose()
            ds = Nothing

            'Signal no error.
            m_error = ""
        Catch ex As Exception
            'Keep the error here.
            m_error = ex.ToString()
        End Try

    End Sub

End Class
